#include "gamescene.h"
#include "gamepanelbutton.h"
#include "unititem.h"
#include "gamepanel.h"
#include "gamefield.h"

GameScene::GameScene(QObject *parent) : QGraphicsScene(parent)
{
    Waypoints m_waypoints;
    m_waypoints << QPoint(90, -50); // additional point
    m_waypoints << QPoint(90, 0);
    m_waypoints << QPoint(90, 386) << QPoint(295, 386);
    m_waypoints << QPoint(295, 114) << QPoint(790, 114) << QPoint(790, 700);
    m_waypoints << QPoint(640, 700) << QPoint(640, 310) << QPoint(450, 310);
    m_waypoints << QPoint(450, 540) << QPoint(90, 540) << QPoint(90, 900);
    m_waypoints << QPoint(230, 900) << QPoint(230, 710) << QPoint(390, 710);
    m_waypoints << QPoint(390, 900) << QPoint(1000, 900);
    m_waypoints << QPoint(1050, 900); // additional point

    m_field = new GameField(QPixmap(":/images/field.png"));
    m_field->setPos(0,0);

    m_field->setWaypoints(m_waypoints);
    addItem(m_field);

    m_panel = new GamePanel();
    addItem(m_panel);

    GamePanelButton * btn = new GamePanelButton(QPixmap(":/images/icon.png"), m_panel);
    btn->setPos(10, 10);
    //btn->setCanToggled(true);
    connect(btn, SIGNAL(pressed()), this, SLOT(test()));

    GamePanelButton * btn2 = new GamePanelButton(QPixmap(":/images/icon.png"), m_panel);
    btn2->setPos(10, 200);
    btn2->setCanToggled(true);
    connect(btn2, SIGNAL(pressed()), this, SLOT(test2()));

    connect(this, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(onSceneRectChanged(QRectF)));
    setSceneRect(QRectF(0, 0, 800, 600));
}

void GameScene::onSceneRectChanged(const QRectF& rect)
{
    m_aspect = rect.width() / rect.height();

    // calculating field rect
    qreal w = rect.width() / m_aspect;
    qreal h = rect.height();
    qreal fFieldSize = qMin(w, h);
    qreal offsetX = 0;
    qreal offsetY = 0;
    if (fFieldSize == w)
    {
        offsetY = (h - fFieldSize) / 2.0;
    }
    else
    {
        offsetX = (w - fFieldSize) / 2.0;
    }
    m_rGameField = QRectF(offsetX, offsetY, fFieldSize, fFieldSize);

    m_field->setScale(m_rGameField.width() / 1000.0);
    m_panel->setPos(m_rGameField.width(), 0);
    m_panel->setSize(rect.width() - m_rGameField.width(), rect.height());
}

GameScene::~GameScene()
{
    delete m_field;
    delete m_panel;
}

void GameScene::setPaused(const bool p_pause, const bool p_fromUser) {
        // If the game has paused
        if (p_pause) {
                // If the pause is due to an action from the user
                if (p_fromUser) {
                        // If the label was not displayed yet
//			if (!items().contains(m_pauseLabel)) {
                                // Display the pause label
//				addItem(m_pauseLabel);
//				m_pauseLabel->setPos((width() - m_pauseLabel->boundingRect().width()) / 2, (height() - m_pauseLabel->boundingRect().height()) / 2);
//                        }
                }
                // Stop kapman animation
//		m_kapmanItem->pauseAnim();
        } else {	// If the game has resumed
                // If the pause was due to an action from the user
                if (p_fromUser) {
                        // If the label was displayed
//			if (items().contains(m_pauseLabel)) {
//				removeItem(m_pauseLabel);
//			}
                }
                // Resume kapman animation
//		m_kapmanItem->resumeAnim();
        }
}

void GameScene::unitGone()
{

}




void GameScene::test()
{
    UnitItem * unit = new UnitItem();
    addItem(unit);
    m_field->startUnit(unit);
}


void GameScene::test2()
{

}
